<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>添加身份信息</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/weui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
    <script src="../script/jquery-3.2.1.js"></script>
    <script src="../script/switch.js"></script>
    <script src="../script/api.js"></script>
    <style>
        .empty{ text-align: center; padding: 120px 0; }
    </style>
</head>
<!-- 添加身份信息页面 -->
<body>
	<div style="display: none" id="countId">id</div>
	<div class="weui_cells_title">姓名</div>
	<div class="weui_cell">
       <div class="weui_cell_bd weui_cell_primary">
           <input class="weui_input" type="text" placeholder="请输入姓名" id="name"/>
       </div>
    </div>
    <div class="weui_cells_title">身份证号</div>
    <div class="weui_cell">
       <div class="weui_cell_bd weui_cell_primary">
           <input class="weui_input" type="text" placeholder="请输入身份证号" id="card_no"/>
       </div>
    </div>
     
    <div class="weui_cell weui_cell_select weui_select_after">
	<div class="weui_cell_hd">民族
	</div>
	<div class="weui_cell_bd weui_cell_primary">
	    <select class="weui_select" name="select1" id="nation">
			<option value="1">汉族</option>
		    <option value="2">蒙古族</option>
		    <option value="3">回族</option>
		    <option value="4">藏族</option>
		    <option value="5">维吾尔族</option>
		    <option value="6">苗族</option>
		    <option value="7">彝族</option>
		    <option value="8">壮族</option>
		    <option value="9">布依族</option>
		    <option value="10">朝鲜族</option>
		    <option value="11">满族</option>
		    <option value="12">侗族</option>
		    <option value="13">瑶族</option>
		    <option value="14">白族</option>
		    <option value="15">土家族</option>
		    <option value="16">哈尼族</option>
		    <option value="17">哈萨克族</option>
		    <option value="18">傣族</option>
		    <option value="19">黎族</option>
		    <option value="20">傈僳族</option>
		    <option value="21">佤族</option>
		    <option value="22">畲族</option>
		    <option value="23">高山族</option>
		    <option value="24">拉祜族</option>
		    <option value="25">水族</option>
		    <option value="26">东乡族</option>
		    <option value="27">纳西族</option>
		    <option value="28">景颇族</option>
		    <option value="29">柯尔克孜族</option>
		    <option value="30">土族</option>
		    <option value="31">达斡尔族</option>
		    <option value="32">仫佬族</option>
		    <option value="33">羌族</option>
		    <option value="34">布朗族</option>
		    <option value="35">撒拉族</option>
		    <option value="36">毛南族</option>
		    <option value="37">仡佬族</option>
		    <option value="38">锡伯族</option>
		    <option value="39">阿昌族</option>
		    <option value="40">普米族</option>
		    <option value="41">塔吉克族</option>
		    <option value="42">怒族</option>
		    <option value="43">乌孜别克族</option>
		    <option value="44">俄罗斯族</option>
		    <option value="45">鄂温克族</option>
		    <option value="46">德昂族</option>
		    <option value="47">保安族</option>
		    <option value="48">裕固族</option>
		    <option value="49">京族</option>
		    <option value="50">塔塔尔族</option>
		    <option value="51">独龙族</option>
		    <option value="52">鄂伦春族</option>
		    <option value="53">赫哲族</option>
		    <option value="54">门巴族</option>
		    <option value="55">珞巴族</option>
		    <option value="56">基诺族</option>
		    <option value="99">其他</option>
	    </select>
	</div>
	</div>
	<div class="weui_cell weui_cell_select weui_select_after">
		<div class="weui_cell_hd">性别</div>
		<div class="weui_cell_bd weui_cell_primary">
		    <select class="weui_select" name="select2" id="gender">
				<option value="1">女</option>
				<option value="2">男</option>
	        </select>
	    </div>
	</div>
	<div class="weui_cells_title">出生日期</div>
	<div class="weui_cell">
        <div class="weui_cell_bd weui_cell_primary">
            <input  class="weui_input" type="text" id="birth" disabled/>
        </div>
    </div>
    <div class="weui_cells_title">详细地址</div>
	<div class="weui_cell">
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="text" placeholder="请输入详细现住址" id="address"/>
        </div>
     </div>
	<br />
	
     <center>
	     <div style="float: left;width: 100%">
		     <a href="../html/frame0.html" style="width: 50%;float: left">
		    	<button class="weui_btn weui_btn_primary" style="background-color:#D3D3D3;
		    	width: 100%;" id = "cancle">返回，不保存</button>
		     </a>
		     <button class="weui_btn weui_btn_primary" 
		     style="width: 50%;background-color: #18b4ed;float: left" onclick="crossDomainRequest()" 
		     id = "save">确定保存</button>
	     </div>
     </center>
</body>
</html>
<script type="text/javascript">
var urlStr = "101.207.250.6:8899";
//获取旅客信息id，如存在id则是修改，不存在则是添加
apiready = function(){
	var id = getQueryString("id"); 
	if(id!=""){
		getMsgByid(id);		 
	}
};
//发起根据id获取身份信息请求
function getMsgByid(id){
	var url = 'http://www.scscpt.com:8520/wzrz/wzrz_getmsgbyid';
	api.ajax({
		    url: url,
		    method: 'post',
		    dataType: 'text',
		    header:{
		    	'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
		    },
	    data: {
	    	values:{
	            id: id
	    	}
	    }
	}, function(ret, err) {
	    if (ret) {
	        var MSG5 = JSON.stringify(ret) ;	    	
		    var str = MSG5.split('*');      
	        var msg = str[1].split("|");
	  		var countid = msg[0];
	  		$("#countId").text(countid);
			$("#name").val(msg[1]);
			$("#card_no").val(msg[2]);
			$("#nation").val(msg[3]);
			$("#gender").val(msg[4]);
			$("#birth").val(msg[5]);
			$("#address").val(msg[6]);
		    $("#save").text("确认修改，并保存");
	    	$("#cancle").text("取消修改");
	    }
	});
	/*
	if (xhr) {
		var uri = 'http://'+urlStr+'/Service1.asmx/wzrz_getMsgById';
	    xhr.open('POST', uri, true);
	    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    xhr.onreadystatechange = handler2;
	    xhr.send("id="+id);
	    $("#save").text("确认修改，并保存");
	    $("#cancle").text("取消修改");
	} else {
	   // alert("不能创建 XMLHttpRequest");
	}
	*/
}
//处理获取身份信息请求结果
function handler2(evtXHR) {
	if (xhr.readyState == 4){
    if (xhr.status == 200){
		var response = xhr.responseText;        
        var str = response.split("*");
        var msg = str[1].split("|");
  		var countid = msg[0];
  		$("#countId").text(countid);
		$("#name").val(msg[1]);
		$("#card_no").val(msg[2]);
		$("#nation").val(msg[3]);
		$("#gender").val(msg[4]);
		$("#birth").val(msg[5]);
		$("#address").val(msg[6]);
		
    } else {
		//alert( "不允许跨域请求。");
    	}
	}
	else {
	}
}
var birth ;
//身份证格式核验
$("#card_no").blur(function() 
{ 
	var card = document.getElementById("card_no").value;
	  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
	var reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
	if(reg.test(card) === false) 
	{ 
		alert("身份证输入不合法,请重新输入！"); 
		document.getElementById("card_no").style.backgroundColor = "#FFB6C1";
	    return false; 
	}else{
		document.getElementById("card_no").style.backgroundColor = "white";
		birth = card.substr(6,8);
		var year = birth.substr(0,4);
		var month = birth.substr(4,2);
		var day = birth.substr(6,2);
		birth = year+"/"+month+"/"+day;
		$('#birth').val(birth);
		return true;
	} 
});
	var xhr = new XMLHttpRequest();
	//添加/修改身份请求
    function crossDomainRequest() {
	    var name = $("#name").val();
		var card_no = $("#card_no").val();
		var nation = $("#nation").val();
		var gender = $("#gender").val();
		var birth = $("#birth").val();
		var address =  $("#address").val();
		var account = $api.getStorage("dl_phone");
		if(name == "" || card_no == ""||nation== ""||gender== ""||birth== ""||address== ""){
		alert("请完整填写您的身份信息");
		return;
		}
		var reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
		if(reg.test(card_no) === false) 
		{ 
			alert("身份证输入不合法,请重新输入！"); 
			document.getElementById("card_no").style.backgroundColor = "#FFB6C1";
		    return false; 
		}
		document.getElementById("card_no").style.backgroundColor = "white";
		var text = document.getElementById("save").innerHTML;
		if(text==="确认修改，并保存"){
			event.returnValue = confirm("确认修改么？");
	    	if(!event.returnValue){
	    		return;
	    	}
			//发起修改请求		
			var countid = document.getElementById("countId").innerHTML;
			var requestMsg=countid+","+name+","+card_no+","+nation+","+gender+","+birth+","+address;
		    var url = 'http://www.scscpt.com:8520/wzrz/wzrz_updatemsg';
			api.ajax({
			    url: url,
			    method: 'post',
			    dataType: 'text',
			    header:{
			    	'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
			    },
		    data: {
		    	values:{
		            request: requestMsg
		    	}
		    }
		}, function(ret, err) {
		    if (ret) {
	            window.location.href="../html/frame0.html" ;
				//window.location.href="../html/frame2.html?id="+Sid+"&hotleName="+hotleName+"&taxcode="+taxcode;
		    } else {
		        api.alert({ msg: JSON.stringify(err) });
		    }
		});/*
			if (xhr) {
			    xhr.open('POST', url, true);
			    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			    xhr.onreadystatechange = handler;
			    xhr.send("request="+countid+","+name+","+card_no+","+nation+","+gender+","+birth+","+address);
			} else {
			   // alert("不能创建 XMLHttpRequest");
			}	
			*/
		}else{
		    var requestMsg =+account+","+name+","+card_no+","+nation+","+gender+","+birth+","+address
		    var url = 'http://www.scscpt.com:8520/wzrz/wzrz_savemsg';
			api.ajax({
				    url: url,
				    method: 'post',
				    dataType: 'text',
				    header:{
				    	'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
				    },
			    data: {
			    	values:{
			            request: requestMsg
			    	}
			    }
			}, function(ret, err) {
			    if (ret) {
		            window.location.href="../html/frame0.html" ;
					//window.location.href="../html/frame2.html?id="+Sid+"&hotleName="+hotleName+"&taxcode="+taxcode;
			    } else {
			        api.alert({ msg: JSON.stringify(err) });
			    }
			});/*
			if (xhr) {
			//发起添加请求
			    xhr.open('POST', url, true);
			    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			    xhr.onreadystatechange = handler;
			    xhr.send("request="+account+","+name+","+card_no+","+nation+","+gender+","+birth+","+address);
			} else {
			   // alert("不能创建 XMLHttpRequest");
			}
			*/	
		}
	}
//处理结果，跳转页面
/*
    function handler(evtXHR) {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var response = xhr.responseText;        
			window.location.href="../html/frame0.html" ;
        } else {
         // alert( "不允许跨域请求。");
        }
      }
      else {
       // alert( "<br/>执行状态 readyState：" + xhr.readyState);
      }
    }
*/
</script>






